<template>
  <div class="addOrderWrap">
    <h3 style="margin:10px 0;font-size:18px">样品申请</h3>
    <el-form
      ref="addOrderForm"
      :model="addOrderForm"
      :rules="addOrderRule"
      label-width="110px"
      label-positon="left"
    >
      <el-form-item prop="account" label="申请账户：">
        <el-input
          v-model="addOrderForm.account"
          disabled
          style="width:40%"
        ></el-input>
      </el-form-item>

      <el-form-item prop="mobile" label="申请电话：">
        <el-input
          v-model="addOrderForm.mobile"
          disabled
          style="width:40%"
        ></el-input>
      </el-form-item>


      <el-form-item prop="company" label="公司名称：">
        <el-input
          v-model="addOrderForm.company"
          style="width:40%"
        ></el-input>
      </el-form-item>

      <el-form-item prop="contact" label="联系人：">
        <el-input
          v-model="addOrderForm.contact"
          style="width:40%"
        ></el-input>
      </el-form-item>

      <el-form-item prop="consignee" label="收货人：">
        <el-input
          v-model="addOrderForm.consignee"
          style="width:40%"
        ></el-input>
      </el-form-item>
       <el-form-item prop="consignee_mobile" label="收货联系方式：">
        <el-input
          v-model="addOrderForm.consignee_mobile"
          style="width:40%"
        ></el-input>
      </el-form-item>

      <el-form-item prop="companyAddress" label="收货地址：">
        <el-input
          v-model="addOrderForm.companyAddress"
          style="width:40%"
        ></el-input>
      </el-form-item>
      <el-form-item prop="remark" label="申请理由：">
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入申请理由"
          v-model="addOrderForm.remark"
          style="width:80%"
        ></el-input>
      </el-form-item>

      <el-form-item prop="manufacturer" label="供应商：">
        <el-input
                v-model="addOrderForm.manufacturer"
                style="width:40%"
                :disabled="true"
        ></el-input>
      </el-form-item>


 
       <el-form-item label="供应商料号" prop="productNum">
          <el-select v-model="addOrderForm.productNum" filterable  placeholder="请选择"   style="width:40%">
            <el-option
              v-for="item in AllSlist"
              :key="item.productCode"
              :label="item.productCode"
              :value="item.productCode"
            ></el-option>
          </el-select>
        </el-form-item>
       
 
      <el-form-item prop="count" label="申请数量">
        <el-input
                v-model="addOrderForm.count"
                style="width:40%"
                type="number"
        ></el-input>
      </el-form-item>
      <el-form-item prop="client" label="终端客户：">
        <el-input
          v-model="addOrderForm.client"
          @blur="querySingleSample"
          style="width:40%"
        ></el-input>
      </el-form-item>
       <el-form-item prop="useProduct" label="项目产品：">
        <el-input
          v-model="addOrderForm.useProduct	"
          style="width:40%"
        ></el-input>
      </el-form-item>
 

      <el-form-item  prop="useTime" label="量产时间：">
        <el-date-picker v-model="addOrderForm.useTime" type="datetime" value-format="timestamp"></el-date-picker>
      </el-form-item>

      <el-form-item prop="useCount" label="单机用量：">
        <el-input
          v-model="addOrderForm.useCount"
          @blur="querySingleSample"
          style="width:40%"
          type="number"
        ></el-input>
      </el-form-item>
       <el-form-item prop="applicateNum" label="预估产量(套)：">
        <el-input
          v-model="addOrderForm.applicateNum"
          @blur="querySingleSample"
          style="width:40%"
          type="number"
        ></el-input>
      </el-form-item>
      <el-form-item prop="useProject" label="项目名称：">
        <el-input
          v-model="addOrderForm.useProject"
          style="width:40%"

        ></el-input>
      </el-form-item>

      <el-form-item prop="pm" label="项目联系人姓名：">
        <el-input
          v-model="addOrderForm.pm"
          style="width:40%"
        ></el-input>
      </el-form-item>
      <el-form-item prop="pmMobile" label="项目联系人电话：">
        <el-input
          v-model="addOrderForm.pmMobile"
          style="width:40%"
        ></el-input>
      </el-form-item>

      <el-form-item prop="email" label="联系邮箱">
        <el-input
          v-model="addOrderForm.email"
          style="width:40%"
        ></el-input>
      </el-form-item>

 


      <el-form-item>
        <div class="flex" style="width:60%;justify-content:space-around">
          <el-button
            style="background: rgb(7, 131, 241);color: #fff;width:30%"
            @click="handleAdd"
          >提交</el-button>
          <el-button
            style="background: rgb(170, 170, 170);color: #fff;width:30%"
            @click="$router.go(-1)"
          >取消</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { GET_SINGLE_PRODUCT,SAMPLE_APPLY,applyr,currentUser,apply2,recordAuth,listByProductCode,GETCODEBYNAME } from '@/utils/api';
export default {
  data() {
    return {
        categroyList: [

        ],
      addOrderForm: {
        sampleId: "",
        sampleName: "",
        manufacturer: "",
        pm: "",
        company:'',
        email:'',
        pmMobile:'',
        contactTel: "",
        conmpanyName: "",
        companyAddress: "",
        price: "",
        applicateNum: "",
        reason: "",
        mobile:'',
        count:'',
        useCount:'',
      },
      AllSlist: [],

      addOrderRule: {
        applicateNum: [
          { required: true, message: "请输入预估产量(套)", trigger: "blur" }
        ],
        contact: [
          { required: true, message: "请输入联系人", trigger: "blur" }
        ],
        consignee: [
          { required: true, message: "请输入收货人", trigger: "blur" }
        ],
        consignee_mobile: [
          { required: true, message: "请输入收货人电话", trigger: "blur" }
        ],
        count: [
          { required: true, message: "请输入申请数量", trigger: "blur" }
        ],
        useTime: [
          { required: true, message: "请选择量产时间", trigger: "blur" }
        ],
        useProject: [
          { required: true, message: "请输入项目名称", trigger: "blur" }
        ],
        productNum: [
          { required: true, message: "请输入申请制造商料号", trigger: "blur" }
        ],
        email: [
          { required: true, message: "请输入联系邮箱", trigger: "blur" }
        ],
        company: [
          { required: true, message: "请输入公司名称", trigger: "blur" }
        ],
        companyAddress: [
          { required: true, message: "请输入收货地址", trigger: "blur" }
        ],
        manufacturer: [
          { required: true, message: "请输入供应商", trigger: "blur" }
        ],
        useCount: [
          { required: true, message: "请输入单机用量", trigger: "blur" }
        ],
        useProduct: [
          { required: true, message: "请输入项目产品", trigger: "blur" }
        ],
        account: [
          { required: true, message: "请输入申请账户", trigger: "blur" }
        ],
        client: [
          { required: true, message: "请输入终端客户", trigger: "blur" }
        ],
        pm: [
          { required: true, message: "请输入PM姓名", trigger: "blur" }
        ],
        pmMobile: [
          { required: true, message: "请输入PM电话", trigger: "blur" }
        ],
          remark: [
          { required: true, message: "请输入申请理由", trigger: "blur" }
        ],
          mobile: [
          { required: true, message: "请输入联系人电话", trigger: "blur" }
        ]
      }
    };
  },
    created(){

        this.currentUser()
        // this.getSampleMyList()
    },
  mounted() {
    if (this.$route.params.orderInfo) {
      let orderInfo = this.$route.params.orderInfo;
      this.addOrderForm = orderInfo;
     
    }
   
  },
  methods: {
     //获取指定供应商下的供应商料号
     getAllList() {
       console.log("获取料号")
      GETCODEBYNAME({
          menuName: this.addOrderForm.manufacturer,
      }).then(res => {
        this.AllSlist = res.data;
        console.log(this.AllSlist);
      });
    },
      changezhou () {

          let productCode=this.addOrderForm.productCode

          let datacount = this.categroyList.length
          let i = 0;
          for (i = 0; i < datacount; i++) {

              if(this.categroyList[i].productCode  == this.addOrderForm.productCode){
                  this.addOrderForm.productid=this.categroyList[i].id
              }

          }
          console.log(  this.addOrderForm.productid)
      },
      onInputBlur(){
          listByProductCode({
              productCode: this.addOrderForm.productNum,
          }).then(res => {
             this.categroyList=res.data
              console.log(this.categroyList)
          })

      },
      currentUser(){

          currentUser({

          }).then(res => {

              this.addOrderForm.account= res.data.userInfo.account
              this.addOrderForm.mobile= res.data.userInfo.mobile
              this.addOrderForm.manufacturer= res.data.menuInfo.menuName
              // this.addOrderForm.id= res.data.menuInfo.menuName
  
          }).then(res2=>{
             this.getAllList()
          })
      },
    // 根据样品ID 查询样品详情
    querySingleSample() {
      // GET_SINGLE_PRODUCT({
      //     productNum : this.addOrderForm.sampleId
      // }).then(res => {
      //   console.log("res : " + JSON.stringify(res))
      //   // 赋值到样品名称
      //   this.addOrderForm.sampleName = res.data.productStoreName
      //   this.addOrderForm.manufacturer = res.data.menuName
      //
      // })
    },
    // 提交样品申请
    handleAdd() {
      // 校验
      this.$refs["addOrderForm"].validate(valid => {
        if (!valid) {
          this.$message({
            message: "请检查填写是否有误！",
            type: "warning"
          });
          return false;
        }
        // 调用
        const req = {
          //wmc
            consignee:this.addOrderForm.consignee,
            consignee_mobile:this.addOrderForm.consignee_mobile,
            useTotal : this.addOrderForm.useTotal,
            client : this.addOrderForm.client,
            useProject:this.addOrderForm.useProject,
            useProduct: this.addOrderForm.useProduct,
            companyAddress: this.addOrderForm.companyAddress,
            company: this.addOrderForm.company,
            remark: this.addOrderForm.remark,
            expressNum: this.addOrderForm.expressNum,
            applicateNum: this.addOrderForm.applicateNum,
            useCount:this.addOrderForm.useCount,
            useTime : this.addOrderForm.useTime/1000,
            count:this.addOrderForm.count,
            productNum:this.addOrderForm.productNum,
            productId:this.addOrderForm.productid,
            manufacturer: this.addOrderForm.manufacturer,
            pm: this.addOrderForm.pm,
            pmMobile: this.addOrderForm.pmMobile,
            mobile: this.addOrderForm.mobile,
            contact:this.addOrderForm.contact
        }
          apply2(req).then(res => {
              if(res.status == 200){
                  this.$message({
                      type: 'success',
                      message: '申请成功'
                  });
                this.$router.push({ path: "exapply" })
              }

          
        })
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.addOrderWrap {
  width: 100%;
  background: #fff;
  padding: 10px;
  margin: 15px 0;
}
</style>